import { Component,Input,OnChanges, OnDestroy } from '@angular/core';

import { BrandMonthHistoryService } from './brand.month.history.service';

import { Util } from '../../../../utils/util';



@Component({
  selector: 'brand-month-history',
  templateUrl: './brand.month.history.html',
  providers: [BrandMonthHistoryService]
})

export class BrandMonthHistoryComponent {


  @Input() date:any;

  brandInfo: any;

  constructor(private service: BrandMonthHistoryService, private util: Util) {

    this.brandInfo = this.util.sessionStore().get('brandDetail');

  }

  vm = {
    "loading": true,
    "data": [],
    "column":{
      fileName:'',
      dim:['date', 'categoryId','brandName', 'monthSellMoney', 'monthSellMoney', 'monthSellAmount', 'productNum', 'shopNum', 'monthTsellMoney', 'monthTsellAmount', 'tproductNum', 'tshopNum', 'monthThkSellMoney', 'monthThkSellAmount', 'thkProductNum', 'thkShopNum', 'monthGsellMoney', 'monthGsellAmount', 'gproductNum', 'gShopNum', 'monthCsellMoney', 'monthCsellAmount', 'cproductNum', 'cshopNum'],
      dimText:['时间', '类目id','品牌名','月销售额', '月销售量', '动销宝贝数', '动销店铺数', '天猫月销售额', '天猫月销售量', '天猫动销宝贝数', '天猫动销店铺数', '天猫国际月销售额', '天猫国际月销售量', '天猫国际动销宝贝数', '天猫国际动销店铺数', '全球购月销售额', '全球购月销售量', '全球购动销宝贝数', '全球购动销店铺数', '淘宝月销售额', '淘宝月销售量', '淘宝动销宝贝数', '淘宝动销店铺数']
    },
    "type": 'line-month'
  };

  columns = ['时间', '品牌名', '月销售量','月销售额', '宝贝数', '店铺数'];

  // change

  ngOnChanges(changes) {

    console.log(changes);

    if (changes.date.currentValue && changes.date.currentValue.timeType =='month') {

      console.log(this.date);
      this.vm.loading = true;
      this.getHttp(this.brandInfo,this.date.date);
    }


  }

  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();
    }
  }

  export() {
    console.log("export");



    let data = this.util.jsonToArrry(this.vm.data, this.vm.column.dim,  this.vm.column.dimText);

    this.util.export(data, this.vm.column.fileName);
  }


  sellResulet:any = {};
  offEvent: any;
  getHttp(brandInfo,date) {

    if(this.vm.loading && this.offEvent){
      this.offEvent.unsubscribe();
    }

    let json = {
      brandName: "'" + brandInfo.brandName + "'",
      categoryId: brandInfo.categoryId || brandInfo.category
    };
    Object.assign(json,date);
    // console.log(json);

    this.sellResulet = {
      'monthSellMoney':0,
      'monthSellAmount':0
    };
    this.vm.column.fileName =  this.brandInfo.brandName+ '~'+(this.brandInfo.categoryName=='/'?"全":this.brandInfo.categoryName) +"类目月销售走势";

    this.offEvent = this.service.get(json).subscribe((res: any) => {


       // 合计
      this.sellResulet = this.util.getSellAllResult(this.sellResulet , res.data);

      this.vm.data = res.data;

      this.vm.loading = false;

    }, () => { });
  }



}
